package com.lanswon.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanswon.common.enums.ArticleStatus;
import com.lanswon.common.enums.ArticleType;
import com.lanswon.common.enums.ProblemType;
import com.lanswon.common.util.StringUtil;
import com.lanswon.entity.ArticleVO;
import com.lanswon.entity.CommonProblem;
import com.lanswon.service.CommonProblemService;

@Controller
@RequestMapping("admin/commonProblem")
public class AdminCommonProblemController {
	
	@Autowired
	private CommonProblemService commonProblemService;
	
	@RequestMapping("cpindex/{problemType}")
	public String cpindex(HttpServletRequest request,HttpServletResponse response,
			@PathVariable(value="problemType")Integer problemType,ModelMap modelMap) {
		Map<String, Object>  accessMap = new HashMap<>();
		accessMap.put("problemType", problemType);
		List<CommonProblem> cpList = commonProblemService.findCommonProblemByCondition(accessMap);
		if(cpList!=null&&cpList.size()>0) {
			modelMap.addAttribute("commonProblem", cpList.get(0));
		}
		modelMap.addAttribute("problemType", problemType);
		
		return "admin/commonProblem/cpindex";
	}
	
	/*@RequestMapping("cpPageList")
	@ResponseBody
	public Map<String, Object> cpPageList(HttpServletRequest request,HttpServletResponse response,Integer page,
			Integer limit,String problemName,Integer problemType){
		Map<String, Object> viewMap = new HashMap<>();
		if(page==null) {
			page = 1;
		}
		if(limit==null) {
			limit = 10;
		}
		Map<String, Object> accessMap = new HashMap<>();
		accessMap.put("currentResult", (page-1)*limit);
		accessMap.put("endResult", limit);
		accessMap.put("problemType", problemType);
		accessMap.put("problemName", problemName);
		Integer count = commonProblemService.pageListCount(accessMap);
		List<CommonProblem> pageList = commonProblemService.pageList(accessMap);
		viewMap.put("totalPages", count!=null?count%limit>0?(count/limit)+1:count/limit:0);
		viewMap.put("data", pageList);
		viewMap.put("code", 0);
		viewMap.put("count", count);  
		viewMap.put("msg", "查询成功");
		return viewMap;
	}*/
	
	
	/*@RequestMapping("cpdetail")
	public String cpdetail(HttpServletRequest request,HttpServletResponse response,Long id,ModelMap modelMap){
		Map<String, Object> viewMap = new HashMap<>();
		
		CommonProblem commonProblem = null;
		if(id!=null) {
			commonProblem = commonProblemService.findCommonProblemById(id);
		}
		modelMap.addAttribute("commonProblem", commonProblem);
		viewMap.put("code", 1);
		viewMap.put("msg", "查询成功");
		return "admin/commonProblem/cpdetail";
	}
	
	
	@RequestMapping("cpdel")
	@ResponseBody
	public Map<String, Object> cpdel(HttpServletRequest request,HttpServletResponse response,Long id){
		Map<String, Object> viewMap = new HashMap<>();
		
		try {
			commonProblemService.deleteCommonProblem(id);
			viewMap.put("code", 1);
			viewMap.put("msg", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			viewMap.put("code", 2);
			viewMap.put("msg", "删除失败");
		}
		return viewMap;
	}
	
	
	@RequestMapping("cpupdate")
	public String cpupdate(HttpServletRequest request,HttpServletResponse response,Long cpId,ModelMap modelMap) {
		
		CommonProblem commonProblem = null;
		if(cpId != null) {
			commonProblem = commonProblemService.findCommonProblemById(cpId);
		}
		modelMap.addAttribute("cpTypes", ProblemType.values());
		modelMap.addAttribute("commonProblem", commonProblem);
		
		return "admin/commonProblem/cpupdate";
	}*/
	
	@RequestMapping("updateCommonProblem")
	@ResponseBody
	public Map<String, Object> updateCommonProblem(HttpServletRequest request,HttpServletResponse response, CommonProblem commonProblem){
		Map<String, Object> viewMap = new HashMap<>();
		try {
			System.out.println(commonProblem.getReleaseTimeStr());
			if(commonProblem.getReleaseTimeStr()!=null) {
				commonProblem.setReleaseTime(StringUtil.strToDate(commonProblem.getReleaseTimeStr(), "yyyy-MM-dd"));
			}
			if(commonProblem.getId()!=null) {
				commonProblemService.updateCommonProblem(commonProblem);
			}else {
				commonProblemService.addCommonProblem(commonProblem);
			}
			viewMap.put("code", 1);
			viewMap.put("msg", "保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			viewMap.put("code", 2);
			viewMap.put("msg", "保存失败");
		}
		return viewMap;
	}
	
}
